package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.*;
import com.hualude.roadsign.service.GuardrailFaultCheckService;
import com.hualude.roadsign.utils.PushUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by ding on 2017/8/15.
 */
@Service
public class GuardrailFaultCheckServiceImpl implements GuardrailFaultCheckService {

    @Resource
    private GuardrailFaultRepository guardrailFaultRepository;
    @Resource
    private GuardrailRepository guardrailRepository;
    @Resource
    private UserRepository userRepository;
    @Resource
    private OrgRepository orgRepository;
    @Resource
    private PushHistoryRepository pushHistoryRepository;

    private String strUserIds;

    private SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    public DTResponseDto getGuardrailFaultCheck4DT(DTRequestDto dtRequestDto, Integer actorId, Integer orgId) throws Exception {
        List<DTGuardrailFaultDto> dto4DTs = guardrailFaultRepository.getGuardrailFaultCheck4DT(dtRequestDto, actorId, orgId);
        Integer count = guardrailFaultRepository.getGuardrailFaultCheckCount4DT(dtRequestDto, actorId, orgId);
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        for(DTGuardrailFaultDto dtMonitorFaultDto :dto4DTs ){
            String creatorName = userRepository.getUserNameByUserId(dtMonitorFaultDto.getGuardrailFaultCreatorId());
            dtMonitorFaultDto.setGuardrailFaultCreatorName(creatorName);
            String checkerName = userRepository.getUserNameByUserId(dtMonitorFaultDto.getGuardrailCheckerId());
            dtMonitorFaultDto.setGuardrailCheckerName(checkerName);
            if(dtMonitorFaultDto.getGuardrailMainCheckerId()!=null){
                String mainCheckerName = userRepository.getUserNameByUserId(dtMonitorFaultDto.getGuardrailMainCheckerId());
                dtMonitorFaultDto.setGuardrailMainCheckerName(mainCheckerName);
            }
        }
        return dtResponseDto;
    }


    @Transactional
    public Integer guardrailFaultAudit(Integer isPass, String feedback, Integer lampFaultId, Integer userId) throws Exception {
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateNowStr = sdf.format(d);
        Integer check = guardrailFaultRepository.getCheck(lampFaultId);
        //check:1,主管在审核
        check = (isPass == 2) ? 1 : check;
        if(check==1){
            if(isPass==1|| isPass == 2){
                final String[] strFaultType = {"损毁", "其他"};
                final GuardrailFaultDto faultDto = guardrailFaultRepository.getInfoByFaultId(lampFaultId);
                final Integer orgId = faultDto.getGuardrailOrgId();
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(orgId, 5);
                final String msg = "护栏 NO：" + faultDto.getGuardrailId() + " " +
                        strFaultType[faultDto.getGuardrailFaultType()];
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(orgId, 5, msg, formatter.format(new Date()),7);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
                if (isPass == 1)
                    return guardrailFaultRepository.faultAudit2Success(isPass,feedback,lampFaultId,userId,dateNowStr);
                else
                    return guardrailFaultRepository.straightFaultAudit(isPass, feedback, lampFaultId, userId, dateNowStr);

            }
            else{
                return guardrailFaultRepository.faultAudit2Fail(isPass,feedback,lampFaultId,userId,dateNowStr);
            }
        }
        else {
            if(isPass==1){
                final GuardrailFaultDto faultDto = guardrailFaultRepository.getInfoByFaultId(lampFaultId);
                List<UserDto> userDtos = userRepository.getUserIdsByOrgIdAndActorId(faultDto.getGuardrailMainOrgId(), 7);
                final String msg = "护栏 NO：" + faultDto.getGuardrailId() + " 待审核";
                strUserIds = "";
                for (UserDto userDto : userDtos) {
                    strUserIds += userDto.getUserId().toString() + ",";
                }
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            if (!strUserIds.equals("")) {
                                strUserIds = strUserIds.substring(0, strUserIds.length() - 1);
                                PushUtil.sendPushToUser(strUserIds, msg);
                                PushHistoryDto pushHistoryDto = new PushHistoryDto(faultDto.getGuardrailMainOrgId(), 7, msg, formatter.format(new Date()),7);
                                pushHistoryRepository.save(pushHistoryDto.toBean());
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
            return guardrailFaultRepository.faultAudit(isPass,feedback,lampFaultId,userId,dateNowStr);
        }
    }

    public List<GuardrailFaultDto> getGuardrailFaultCheckList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception {
        return guardrailFaultRepository.getGuardrailFaultCheckList4App(orgId, actorIds, faultStatus, count, size);
    }

    public GuardrailFaultDto getGuardrailInfo2Fault(Integer roadLineId) throws Exception {
        GuardrailFaultDto monitorFaultDto = guardrailRepository.getGuardrailInfo2Fault(roadLineId);
        monitorFaultDto.setGuardrailCreatorName(userRepository.getUserNameByUserId(monitorFaultDto.getGuardrailCreatorId()));
        monitorFaultDto.setGuardrailOrgName(orgRepository.getMainOrgName(monitorFaultDto.getGuardrailOrgId()));
        monitorFaultDto.setGuardrailMainOrgName(orgRepository.getMainOrgName(monitorFaultDto.getGuardrailMainOrgId()));
        monitorFaultDto.setGuardrailId(roadLineId);
        return monitorFaultDto;
    }

    public GuardrailFaultDto getGuardrailFaultInfo2Fault(Integer guardrailId, Integer guardrailFaultId) throws Exception {
        GuardrailFaultDto signalLampFaultDto = guardrailRepository.getGuardrailInfo2Fault(guardrailId);
        GuardrailFaultDto guardrailFaultDto = guardrailFaultRepository.getGuardrailFaultInfo2Fault(guardrailFaultId);
        GuardrailFaultDto latlngDto = guardrailFaultRepository.getGuardrailLatLng(guardrailId);
        guardrailFaultDto.setLongitude(latlngDto.getLongitude());
        guardrailFaultDto.setLatitude(latlngDto.getLatitude());
        guardrailFaultDto.setGuardrailProvince(signalLampFaultDto.getGuardrailProvince());
        guardrailFaultDto.setGuardrailCity(signalLampFaultDto.getGuardrailCity());
        guardrailFaultDto.setGuardrailArea(signalLampFaultDto.getGuardrailArea());
        guardrailFaultDto.setGuardrailStartRoad(signalLampFaultDto.getGuardrailStartRoad());
        guardrailFaultDto.setGuardrailEndRoad(signalLampFaultDto.getGuardrailEndRoad());
        guardrailFaultDto.setGuardrailRoadName(signalLampFaultDto.getGuardrailRoadName());
        guardrailFaultDto.setGuardrailOrgId(signalLampFaultDto.getGuardrailOrgId());
        guardrailFaultDto.setGuardrailMainOrgId(signalLampFaultDto.getGuardrailMainOrgId());
        UserDto userDto = userRepository.getUserById(guardrailFaultDto.getGuardrailFaultCreatorId());
        guardrailFaultDto.setGuardrailFaultCreatorName(userDto.getUserName());
        guardrailFaultDto.setGuardrailFaultCreatorPhone(userDto.getUserMobile());
        return guardrailFaultDto;
    }

    public List<GuardrailFaultDto> getGuardrailFaultReportList4App(Integer orgId, String actorIds, Integer faultStatus, Integer count, Integer size) throws Exception {
        return guardrailFaultRepository.getGuardrailFaultReportList4App(orgId,actorIds,faultStatus,count,size);
    }
}
